package com.sinodata.bsm.cicp.collector.tomcat;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.JmxUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class TomcatManagerCollector extends Collector {
    private static Logger logger = Logger.getLogger(TomcatManagerCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        //        String port = this.getParamMap().get("端口");
        String port = this.getParamMap().get("Port");
        String[] propertyNames = new String[] { "activeSessions", "sessionMaxAliveTime", "sessionAverageAliveTime", "maxActive", "rejectedSessions", "expiredSessions", "distributable", "maxInactiveInterval" };
        Object propertyValues[] = null;
        //        String name = this.getParamMap().get("名称").trim();
        String name = this.getParamMap().get("Name").trim();
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];

        JMXConnector connector = null;
        MBeanServerConnection connection = null;
        try {
            //connector = JmxUtil.getConnection(JmxUtil.TOMCAT, ip + ":" + port);
            connector = JmxUtil.getConnection(ip, Integer.parseInt(port));
            connection = connector.getMBeanServerConnection();
            ObjectName tomcatManager = new ObjectName("Catalina:type=Manager,*");
            Set<ObjectName> set = connection.queryNames(tomcatManager, null);
            for (ObjectName objectName : set) {
                if (objectName.getKeyProperty("path").equals(name)) {
                    ObjectName objname = new ObjectName(objectName.getCanonicalName());
                    propertyValues = JmxUtil.getAttribute(connection, objname, propertyNames);

                    for (int i = 0; i < propertyValues.length; i++) {
                        propValues[i] = ResPropertyValue.createPropValue(propertyValues[i].toString(), propMetas[i]);
                    }

                }
            }
        } catch (Exception e) {
            logger.error("when TomcatManagerCollector" + e);
            throw new CollectorException(e);
        } finally {
            JmxUtil.closeConnector(connector);
        }

        propertyValueX.setValues(propValues);
        values.add(propertyValueX);
        return values;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "名称", "端口" };
        return new String[] { "Name", "Port" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "活动会话数量", "最大会话活动时间", "平均会话活动时间", "活动会话数量峰值", "拒绝会话数量", "超时会话数量", "是否分布", "最大无活动等待时间" };
        //return new String[] { "ActiveSessionsNum", "MaxSessionActiveTime", "AvgSessionActiveTime", "ActiveSessionPeak", "RefuseSessionsNum", "OvertimeSessionsNum", "WhetherDistributeOrNot", "MaxActivityWaitTime" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6", "$7", "$8" };
    }

    /*
     * public static void main(String[] args){ TomcatManagerCollector test = new
     * TomcatManagerCollector(); ResValue resValue = new ResValue();
     * ResAttributeValValue[] resAttributeValueValue = new
     * ResAttributeValValue[0]; ResAttributeValue[] resAttributeValue = new
     * ResAttributeValue[0]; //test.collect(resAttributeValueValue, ""); }
     */
}
